package com.probiz.estore.product.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductPackageItem;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductPackageItemDao;
import com.probiz.estore.product.service.ProductPackageItemManager;
import com.probiz.estore.product.service.ProductSkuManager;


/**
 * Manager implementation for ProductPackageItem, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductPackageItemManagerImpl extends GenericManagerImpl<ProductPackageItem> implements ProductPackageItemManager {

	private ProductSkuManager	productSkuManager	= null;
	private ProductPackageItemDao productPackageItemDao = null;

	public void setProductPackageItemDao(ProductPackageItemDao productPackageItemDao) {
		this.productPackageItemDao = productPackageItemDao;
	}
	
	public void setProductSkuManager(ProductSkuManager productSkuManager) {
		this.productSkuManager = productSkuManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productPackageItemDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductPackageItem entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductPackageItem entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductPackageItemManager#saveProductPackageItemFromArrays(java.lang.Integer, java.lang.Integer[], java.lang.Integer[])
	 */
	public List<String> saveProductPackageItemFromArrays(Integer packageSkuId,Integer[] itemSkuIds,Integer[] quantitys){
		List<String> newProductPackageItems = new ArrayList<String>();
		ProductSku productSku=productSkuManager.getById(packageSkuId);
		Set<ProductPackageItem>productPackageItems=productSku.getProductPackageItems();
		if(productPackageItems==null)productPackageItems=new HashSet<ProductPackageItem>();
		//删除没有关联的Item
		for (ProductPackageItem productPackageItem : productPackageItems) {
			boolean exist =ArrayUtils.contains(itemSkuIds, productPackageItem.getItemSkuId());
			if(!exist){
				delete(productPackageItem);
			}
		}
		//新增或更新Item
		for (int i = 0; i < itemSkuIds.length; i++) {
			Integer itemSkuId = itemSkuIds[i];
			ProductPackageItem currentProductPackageItem=null;
			for (ProductPackageItem productPackageItem : productPackageItems) {
				if(itemSkuId.intValue()==productPackageItem.getItemSkuId().intValue()){
					currentProductPackageItem=productPackageItem;
					break;
				}
			}
			if(currentProductPackageItem==null){
				currentProductPackageItem=new ProductPackageItem();
				currentProductPackageItem.setProductSku(productSku);
				currentProductPackageItem.setItemSkuId(itemSkuId);
			}
			currentProductPackageItem.setQuantity(quantitys[i]);
			currentProductPackageItem.setSortOrder(i);
			save(currentProductPackageItem);
			newProductPackageItems.add(currentProductPackageItem.getProductPackageItemId().toString());
		}
		return newProductPackageItems;
		
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductPackageItemManager#getProductPackageByItemProductId(java.lang.Integer)
	 */
	public List<Product> getProductPackageByItemProductId(Integer itemProductId) {
		return productPackageItemDao.getProductPackageByItemProductId(itemProductId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductPackageItemManager#getActiveProductPackageByItemProductId(java.lang.Integer)
	 */
	public List<Product> getActiveProductPackageByItemProductId(Integer itemProductId){
		return productPackageItemDao.getActiveProductPackageByItemProductId(itemProductId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductPackageItemManager#getProductPackageByItemSkuId(java.lang.Integer)
	 */
	public List<Product> getProductPackageByItemSkuId(Integer itemSkuId){
		return productPackageItemDao.getProductPackageByItemSkuId(itemSkuId);
	}


	

}
